<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>Spring源码 - AOP Advisor的封装与搜集 - 暗夜零星</title><meta name="Description" content="AOP Advisor 的搜集"><meta property="og:title" content="Spring源码 - AOP Advisor的封装与搜集" />
<meta property="og:description" content="AOP Advisor 的搜集" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://wlizhi.cc/posts/spring/10-aop-advisor-collect/" /><meta property="og:image" content="https://wlizhi.cc/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2020-11-30T18:01:17+08:00" />
<meta property="article:modified_time" content="2020-11-30T18:01:17+08:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://wlizhi.cc/logo.png"/>

<meta name="twitter:title" content="Spring源码 - AOP Advisor的封装与搜集"/>
<meta name="twitter:description" content="AOP Advisor 的搜集"/>
<meta name="application-name" content="暗夜零星">
<meta name="apple-mobile-web-app-title" content="暗夜零星"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://wlizhi.cc/posts/spring/10-aop-advisor-collect/" /><link rel="prev" href="https://wlizhi.cc/posts/spring/09-aop-support-register/" /><link rel="next" href="https://wlizhi.cc/posts/spring/11-aop-proxy-create-processor/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Spring源码 - AOP Advisor的封装与搜集",
        "inLanguage": "",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/wlizhi.cc\/posts\/spring\/10-aop-advisor-collect\/"
        },"genre": "posts","keywords": "spring源码","wordcount":  6003 ,
        "url": "https:\/\/wlizhi.cc\/posts\/spring\/10-aop-advisor-collect\/","datePublished": "2020-11-30T18:01:17+08:00","dateModified": "2020-11-30T18:01:17+08:00","publisher": {
            "@type": "Organization",
            "name": "Wlizhi"},"author": {
                "@type": "Person",
                "name": "Wlizhi"
            },"description": "AOP Advisor 的搜集"
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="暗夜零星"><span class="header-title-pre"><i class='far fa-moon fa-fw'></i></span>暗夜零星</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/message-board/"> 留言 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/wlizhi" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i>  </a><a class="menu-item" href="https://gitee.com/wlizhi" title="Gitee" rel="noopener noreffer" target="_blank"><i class='fab fa-git fa-fw'></i>  </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="点击搜索..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="暗夜零星"><span class="header-title-pre"><i class='far fa-moon fa-fw'></i></span>暗夜零星</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="点击搜索..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/message-board/" title="">留言</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/wlizhi" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i></a><a class="menu-item" href="https://gitee.com/wlizhi" title="Gitee" rel="noopener noreffer" target="_blank"><i class='fab fa-git fa-fw'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">Spring源码 - AOP Advisor的封装与搜集</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="https://wlizhi.cc" title="Author" target="_blank" rel="noopener noreffer author" class="author"><i class="fas fa-user-circle fa-fw"></i>Wlizhi</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/spring/"><i class="far fa-folder fa-fw"></i>spring</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2020-11-30">2020-11-30</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 6003 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 12 分钟&nbsp;<span id="/posts/spring/10-aop-advisor-collect/" class="leancloud_visitors" data-flag-title="Spring源码 - AOP Advisor的封装与搜集">
                        <i class="far fa-eye fa-fw"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
                    </span>&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#1-主流程">1 主流程</a></li>
    <li><a href="#2-搜集封装advisor">2 搜集封装Advisor</a>
      <ul>
        <li><a href="#21-搜集流程">2.1 搜集流程</a></li>
        <li><a href="#22-pointcut的获取">2.2 PointCut的获取</a></li>
        <li><a href="#23-advisor的初始化">2.3 Advisor的初始化</a></li>
      </ul>
    </li>
    <li><a href="#3-筛选出匹配的advisor">3 筛选出匹配的Advisor</a></li>
    <li><a href="#4-搜集容器中所有的advisor的流程">4 搜集容器中所有的Advisor的流程</a></li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><p>spring源码系列文章，示例代码的中文注释，均是 copy 自 <a href="https://gitee.com/wlizhi/spring-framework" target="_blank" rel="noopener noreffer">https://gitee.com/wlizhi/spring-framework</a> 。</p>
<p>链接中源码是作者从 github 下载，并以自身理解对核心流程及主要节点做了详细的中文注释。</p>
<hr>
<h2 id="1-主流程">1 主流程</h2>
<p>AOP是通过 BeanPostProcessor.postProcessAfterInitialization() 实现的，来到 doCreateBean() -&gt; initializeBean()，在初始化方法执行完成后，会调用 applyBeanPostProcessorsAfterInitialization()，这里循环调用了所有 BeanPostProcessor 的 postProcessAfterInitialization()，AOP 的入口就在其中的一个实现类，也是 AbstractAutoProxyCreator 的子类实现。</p>
<p>在生成代理之前，spring 会根据扫描规则，搜集容器中注册的所有 Advisor 实例、并搜集通知方法（@Aspect 注解的类中的@Before、@Around等注解标识的方法），将其封装成 Advisor。然后逐个与当前 bean 实例匹配，匹配到的放入一个集合中。</p>
<p>来到入口方法 postProcessAfterInitialization()，源码如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AbstractAutoProxyCreator</span> <span class="kd">extends</span> <span class="n">ProxyProcessorSupport</span>
</span></span><span class="line"><span class="cl">		<span class="kd">implements</span> <span class="n">SmartInstantiationAwareBeanPostProcessor</span><span class="o">,</span> <span class="n">BeanFactoryAware</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="n">Object</span> <span class="nf">postProcessAfterInitialization</span><span class="o">(</span><span class="nd">@Nullable</span> <span class="n">Object</span> <span class="n">bean</span><span class="o">,</span> <span class="n">String</span> <span class="n">beanName</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">bean</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 如果是FactoryBean，cacheKey是 &amp;+beanName拼接而成，如果benaName为空，则是类名称。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">Object</span> <span class="n">cacheKey</span> <span class="o">=</span> <span class="n">getCacheKey</span><span class="o">(</span><span class="n">bean</span><span class="o">.</span><span class="na">getClass</span><span class="o">(),</span> <span class="n">beanName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(!</span><span class="k">this</span><span class="o">.</span><span class="na">earlyProxyReferences</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">cacheKey</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="c1">// TODO 必要时包装给定的bean，即是否有资格被代理。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>				<span class="k">return</span> <span class="n">wrapIfNecessary</span><span class="o">(</span><span class="n">bean</span><span class="o">,</span> <span class="n">beanName</span><span class="o">,</span> <span class="n">cacheKey</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">bean</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>如果需要生成代理（自定义切面、事务等），会调用 wrapIfNecessary()。</p>
<p>wrapIfNecessary() 是这么定义的：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="hl"><span class="lnt"> 7
</span></span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="hl"><span class="lnt">11
</span></span><span class="hl"><span class="lnt">12
</span></span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AbstractAutoProxyCreator</span> <span class="kd">extends</span> <span class="n">ProxyProcessorSupport</span>
</span></span><span class="line"><span class="cl">		<span class="kd">implements</span> <span class="n">SmartInstantiationAwareBeanPostProcessor</span><span class="o">,</span> <span class="n">BeanFactoryAware</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">protected</span> <span class="n">Object</span> <span class="nf">wrapIfNecessary</span><span class="o">(</span><span class="n">Object</span> <span class="n">bean</span><span class="o">,</span> <span class="n">String</span> <span class="n">beanName</span><span class="o">,</span> <span class="n">Object</span> <span class="n">cacheKey</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 省略无关代码...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// TODO 重点：如果有需要增强，就创建代理对象，这里会循环此类中所有的方法，如果有增强匹配到类中的方法，就会将增强对象封装到list中。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// Create proxy if we have advice.
</span></span></span><span class="line hl"><span class="cl"><span class="c1"></span>		<span class="n">Object</span><span class="o">[]</span> <span class="n">specificInterceptors</span> <span class="o">=</span> <span class="n">getAdvicesAndAdvisorsForBean</span><span class="o">(</span><span class="n">bean</span><span class="o">.</span><span class="na">getClass</span><span class="o">(),</span> <span class="n">beanName</span><span class="o">,</span> <span class="kc">null</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">specificInterceptors</span> <span class="o">!=</span> <span class="n">DO_NOT_PROXY</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">this</span><span class="o">.</span><span class="na">advisedBeans</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">cacheKey</span><span class="o">,</span> <span class="n">Boolean</span><span class="o">.</span><span class="na">TRUE</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// TODO 重点：创建代理对象
</span></span></span><span class="line hl"><span class="cl"><span class="c1"></span>			<span class="n">Object</span> <span class="n">proxy</span> <span class="o">=</span> <span class="n">createProxy</span><span class="o">(</span>
</span></span><span class="line hl"><span class="cl">					<span class="n">bean</span><span class="o">.</span><span class="na">getClass</span><span class="o">(),</span> <span class="n">beanName</span><span class="o">,</span> <span class="n">specificInterceptors</span><span class="o">,</span> <span class="k">new</span> <span class="n">SingletonTargetSource</span><span class="o">(</span><span class="n">bean</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">			<span class="k">this</span><span class="o">.</span><span class="na">proxyTypes</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">cacheKey</span><span class="o">,</span> <span class="n">proxy</span><span class="o">.</span><span class="na">getClass</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="n">proxy</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">this</span><span class="o">.</span><span class="na">advisedBeans</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">cacheKey</span><span class="o">,</span> <span class="n">Boolean</span><span class="o">.</span><span class="na">FALSE</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">bean</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
<div class="details admonition note open">
        <div class="details-summary admonition-title">
            <i class="icon fas fa-pencil-alt fa-fw"></i>注意<i class="details-icon fas fa-angle-right fa-fw"></i>
        </div>
        <div class="details-content">
            <div class="admonition-content"><p>从源码中可以看到，关键点有两个：</p>
<ol>
<li>搜集容器中所有匹配当前bean的通知方法。（如果当前bean中有方法被 pointCut 匹配到，就满足条件，将对应的通知方法封装到数组中返回）</li>
<li>根据匹配的通知方法列表，创建代理对象。（这里会将已排序的通知方法列表，生成一个执行链，缓存起来，当代理对象方法被调用时调用回调方法的时候，会火炬传递式的调用执行链中的通知方法。JDK动态代理和cgliib封装方式大同小异，通知方法都在回调函数所在的类中作为一个成员缓存。）</li>
</ol>
</div>
        </div>
    </div></p>
<h2 id="2-搜集封装advisor">2 搜集封装Advisor</h2>
<h3 id="21-搜集流程">2.1 搜集流程</h3>
<p>getAdvicesAndAdvisorsForBean() 会调用到 findEligibleAdvisors()。来到这个方法：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AbstractAdvisorAutoProxyCreator</span> <span class="kd">extends</span> <span class="n">AbstractAutoProxyCreator</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="kd">protected</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="nf">findEligibleAdvisors</span><span class="o">(</span><span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">beanClass</span><span class="o">,</span> <span class="n">String</span> <span class="n">beanName</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">//查找到所有的增强方法，封装成Advisor对象。这里查找了两种增强，一种是实现了Advisor的实例，一种是带有@Aspect注解的bean实例中定义的增强方法。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">candidateAdvisors</span> <span class="o">=</span> <span class="n">findCandidateAdvisors</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 根据每个增强中的切点表达式，进行匹配，筛选出合适的增强实例列表。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">eligibleAdvisors</span> <span class="o">=</span> <span class="n">findAdvisorsThatCanApply</span><span class="o">(</span><span class="n">candidateAdvisors</span><span class="o">,</span> <span class="n">beanClass</span><span class="o">,</span> <span class="n">beanName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="n">extendAdvisors</span><span class="o">(</span><span class="n">eligibleAdvisors</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 对增强方法进行排序，可以不看。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">if</span> <span class="o">(!</span><span class="n">eligibleAdvisors</span><span class="o">.</span><span class="na">isEmpty</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">eligibleAdvisors</span> <span class="o">=</span> <span class="n">sortAdvisors</span><span class="o">(</span><span class="n">eligibleAdvisors</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">eligibleAdvisors</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>这里分为了两步：</p>
<ol>
<li>收集到容器中所有的增强方法、封装成 Advisor（如果本身就是 Advisor，直接返回）。</li>
<li>从这所有的 Advisor 中筛选出匹配当前 bean 的 Advisor 列表。</li>
</ol>
<p>从 <a href="posts/spring/09-aop-support-register/" rel="">AbstractAutoProxyCreator的注册</a> 已经知道，这里使用的是 AnnotationAwareAspectJAutoProxyCreator。</p>
<p>进入 findCandidateAdvisors()：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">AnnotationAwareAspectJAutoProxyCreator</span> <span class="kd">extends</span> <span class="n">AspectJAwareAdvisorAutoProxyCreator</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">	<span class="kd">protected</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="nf">findCandidateAdvisors</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// TODO 从父类中获取，这里获取了所有注入到容器中的Advisor类型的实例。事务的增强实例，就是在这里查找的。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// Add all the Spring advisors found according to superclass rules.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">advisors</span> <span class="o">=</span> <span class="kd">super</span><span class="o">.</span><span class="na">findCandidateAdvisors</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// Build Advisors for all AspectJ aspects in the bean factory.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">aspectJAdvisorsBuilder</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// TODO 将beanFactory中缓存的所有bean实例，依次校验是否带有@Aspect注解，如果有，将其中的
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="c1">//  增强方法、切点表达式等信息，封装成Advisor。advisors变量最终存储了所有切面的增强方法封装。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">advisors</span><span class="o">.</span><span class="na">addAll</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">aspectJAdvisorsBuilder</span><span class="o">.</span><span class="na">buildAspectJAdvisors</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">advisors</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>看上面代码，首先，从父类中查找 Advisor列表，然后调用 buildAspectJAdvisors()，将两者结果聚合到一个列表。</p>
<hr>
<p>先看父类中的代码：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BeanFactoryAdvisorRetrievalHelper</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="nf">findAdvisorBeans</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="n">String</span><span class="o">[]</span> <span class="n">advisorNames</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">cachedAdvisorBeanNames</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="o">(</span><span class="n">advisorNames</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="c1">// 从容器中获取所有的Advisor类型的额bean名称
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">advisorNames</span> <span class="o">=</span> <span class="n">BeanFactoryUtils</span><span class="o">.</span><span class="na">beanNamesForTypeIncludingAncestors</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">					<span class="k">this</span><span class="o">.</span><span class="na">beanFactory</span><span class="o">,</span> <span class="n">Advisor</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="kc">true</span><span class="o">,</span> <span class="kc">false</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">this</span><span class="o">.</span><span class="na">cachedAdvisorBeanNames</span> <span class="o">=</span> <span class="n">advisorNames</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// 省略非关键代码...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">advisors</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">		<span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">name</span> <span class="o">:</span> <span class="n">advisorNames</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(</span><span class="n">isEligibleBean</span><span class="o">(</span><span class="n">name</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="c1">// 从beanFactory中获取所有的Advisor类型的实例，事务支持TransactionAttributeSourceAdvisor实际上就是一个Advisor。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>                <span class="n">advisors</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">beanFactory</span><span class="o">.</span><span class="na">getBean</span><span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">Advisor</span><span class="o">.</span><span class="na">class</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">advisors</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>这里搜集了容器中所有 Advisor 类型的 beanName，并将其缓存。然后调用 getBean()，将其实例化。（<code>事务的 Advisor 其实就是在这里创建的</code>）</p>
<hr>
<p>再看子类中的代码 buildAspectJAdvisors()：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BeanFactoryAspectJAdvisorsBuilder</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="nf">buildAspectJAdvisors</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">aspectNames</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">aspectBeanNames</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 第一次进来时为空，这里用到了双检索
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">if</span> <span class="o">(</span><span class="n">aspectNames</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="kd">synchronized</span> <span class="o">(</span><span class="k">this</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">aspectNames</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">aspectBeanNames</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">				<span class="k">if</span> <span class="o">(</span><span class="n">aspectNames</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">advisors</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">					<span class="n">aspectNames</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">					<span class="n">String</span><span class="o">[]</span> <span class="n">beanNames</span> <span class="o">=</span> <span class="n">BeanFactoryUtils</span><span class="o">.</span><span class="na">beanNamesForTypeIncludingAncestors</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">							<span class="k">this</span><span class="o">.</span><span class="na">beanFactory</span><span class="o">,</span> <span class="n">Object</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="kc">true</span><span class="o">,</span> <span class="kc">false</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">					<span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">beanName</span> <span class="o">:</span> <span class="n">beanNames</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">						<span class="c1">// 如果不是合格的beanName，则跳过
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>						<span class="k">if</span> <span class="o">(!</span><span class="n">isEligibleBean</span><span class="o">(</span><span class="n">beanName</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">							<span class="k">continue</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">						<span class="o">}</span>
</span></span><span class="line"><span class="cl">						<span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">beanType</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">beanFactory</span><span class="o">.</span><span class="na">getType</span><span class="o">(</span><span class="n">beanName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">						<span class="k">if</span> <span class="o">(</span><span class="n">beanType</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">							<span class="k">continue</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">						<span class="o">}</span>
</span></span><span class="line"><span class="cl">						<span class="c1">// 判断当前beanType是否是一个切面。判断类上是否有@Aspect
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>						<span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">advisorFactory</span><span class="o">.</span><span class="na">isAspect</span><span class="o">(</span><span class="n">beanType</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">							<span class="n">aspectNames</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">beanName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">							<span class="c1">// 创建切面元数据实例，里面封装了切面类型，切点表达式等信息。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>							<span class="n">AspectMetadata</span> <span class="n">amd</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AspectMetadata</span><span class="o">(</span><span class="n">beanType</span><span class="o">,</span> <span class="n">beanName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">							<span class="c1">// 默认就是SINGLETON
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>							<span class="k">if</span> <span class="o">(</span><span class="n">amd</span><span class="o">.</span><span class="na">getAjType</span><span class="o">().</span><span class="na">getPerClause</span><span class="o">().</span><span class="na">getKind</span><span class="o">()</span> <span class="o">==</span> <span class="n">PerClauseKind</span><span class="o">.</span><span class="na">SINGLETON</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">								<span class="n">MetadataAwareAspectInstanceFactory</span> <span class="n">factory</span> <span class="o">=</span>
</span></span><span class="line"><span class="cl">										<span class="k">new</span> <span class="n">BeanFactoryAspectInstanceFactory</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">beanFactory</span><span class="o">,</span> <span class="n">beanName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">								<span class="c1">// TODO 获取Advisor，这里封装了当前Aspect类中的增强方法，每个增强方法都会封装成一个Advisor。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>								<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">classAdvisors</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">advisorFactory</span><span class="o">.</span><span class="na">getAdvisors</span><span class="o">(</span><span class="n">factory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">								<span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">beanFactory</span><span class="o">.</span><span class="na">isSingleton</span><span class="o">(</span><span class="n">beanName</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">									<span class="c1">// 将Advisor放入到缓存中，从上面的流程获取时，会先走缓存，缓存中有，就不会遍历查找了。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>									<span class="c1">// 这个代码块使用了双检索，这块代码在spring容器启动时，只会执行一次。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>									<span class="k">this</span><span class="o">.</span><span class="na">advisorsCache</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">beanName</span><span class="o">,</span> <span class="n">classAdvisors</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">								<span class="o">}</span>
</span></span><span class="line"><span class="cl">								<span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">									<span class="k">this</span><span class="o">.</span><span class="na">aspectFactoryCache</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">beanName</span><span class="o">,</span> <span class="n">factory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">								<span class="o">}</span>
</span></span><span class="line"><span class="cl">								<span class="n">advisors</span><span class="o">.</span><span class="na">addAll</span><span class="o">(</span><span class="n">classAdvisors</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">							<span class="o">}</span>
</span></span><span class="line"><span class="cl">							<span class="c1">// 省略...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>						<span class="o">}</span>
</span></span><span class="line"><span class="cl">					<span class="o">}</span>
</span></span><span class="line"><span class="cl">					<span class="k">this</span><span class="o">.</span><span class="na">aspectBeanNames</span> <span class="o">=</span> <span class="n">aspectNames</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">					<span class="k">return</span> <span class="n">advisors</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">				<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 省略...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// 根据切面名称，从缓存中获取到指定的增强实例，封装起来返回。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">advisors</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">		<span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">aspectName</span> <span class="o">:</span> <span class="n">aspectNames</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">cachedAdvisors</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">advisorsCache</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">aspectName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(</span><span class="n">cachedAdvisors</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">advisors</span><span class="o">.</span><span class="na">addAll</span><span class="o">(</span><span class="n">cachedAdvisors</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">MetadataAwareAspectInstanceFactory</span> <span class="n">factory</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">aspectFactoryCache</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">aspectName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="n">advisors</span><span class="o">.</span><span class="na">addAll</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">advisorFactory</span><span class="o">.</span><span class="na">getAdvisors</span><span class="o">(</span><span class="n">factory</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">advisors</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>上面代码略长、主要看带有注释的地方。代码主要流程：</p>
<ol>
<li>获取到容器中所有的 beanName，遍历。</li>
<li>挨个判断，如果当前类上有 @Aspect，则解析这个类中通知方法及切入点表达式等信息。</li>
<li>调用 getAdvisors() 获取类中所有的通知方法，将其封装成 Advisor，作为一个列表返回。</li>
<li>放入缓存，下次进入就不会再遍历所有 beanNames 计算，而是从缓存中获取。</li>
</ol>
<p>重点就在 getAdvisors()，在此之前，先来看看 isAspect()：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AbstractAspectJAdvisorFactory</span> <span class="kd">implements</span> <span class="n">AspectJAdvisorFactory</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">isAspect</span><span class="o">(</span><span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">clazz</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 这里判断类型是否包含@Aspect
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">return</span> <span class="o">(</span><span class="n">hasAspectAnnotation</span><span class="o">(</span><span class="n">clazz</span><span class="o">)</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">compiledByAjc</span><span class="o">(</span><span class="n">clazz</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="kd">private</span> <span class="kt">boolean</span> <span class="nf">hasAspectAnnotation</span><span class="o">(</span><span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">clazz</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="o">(</span><span class="n">AnnotationUtils</span><span class="o">.</span><span class="na">findAnnotation</span><span class="o">(</span><span class="n">clazz</span><span class="o">,</span> <span class="n">Aspect</span><span class="o">.</span><span class="na">class</span><span class="o">)</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>isAspect() 中的逻辑非常简单，判断当前类中是否有 @Aspect 注解（第二个条件可以忽略，判断类中是否有字段以 <code>ajc$</code> 一个特殊的标识开头的）。</p>
<p>来到 getAdvisors()：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ReflectiveAspectJAdvisorFactory</span> <span class="kd">extends</span> <span class="n">AbstractAspectJAdvisorFactory</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="nf">getAdvisors</span><span class="o">(</span><span class="n">MetadataAwareAspectInstanceFactory</span> <span class="n">aspectInstanceFactory</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 获取切面类型，切面名称
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">aspectClass</span> <span class="o">=</span> <span class="n">aspectInstanceFactory</span><span class="o">.</span><span class="na">getAspectMetadata</span><span class="o">().</span><span class="na">getAspectClass</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="n">String</span> <span class="n">aspectName</span> <span class="o">=</span> <span class="n">aspectInstanceFactory</span><span class="o">.</span><span class="na">getAspectMetadata</span><span class="o">().</span><span class="na">getAspectName</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="n">validate</span><span class="o">(</span><span class="n">aspectClass</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		
</span></span><span class="line"><span class="cl">		<span class="n">MetadataAwareAspectInstanceFactory</span> <span class="n">lazySingletonAspectInstanceFactory</span> <span class="o">=</span>
</span></span><span class="line"><span class="cl">				<span class="k">new</span> <span class="n">LazySingletonAspectInstanceFactoryDecorator</span><span class="o">(</span><span class="n">aspectInstanceFactory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">advisors</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 获取增强方法，并按照方法名称排序。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">for</span> <span class="o">(</span><span class="n">Method</span> <span class="n">method</span> <span class="o">:</span> <span class="n">getAdvisorMethods</span><span class="o">(</span><span class="n">aspectClass</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// TODO 将增强方法封装成Advisor，并添加到advisors变量中，最终会将这个变量返回。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">Advisor</span> <span class="n">advisor</span> <span class="o">=</span> <span class="n">getAdvisor</span><span class="o">(</span><span class="n">method</span><span class="o">,</span> <span class="n">lazySingletonAspectInstanceFactory</span><span class="o">,</span> <span class="n">advisors</span><span class="o">.</span><span class="na">size</span><span class="o">(),</span> <span class="n">aspectName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(</span><span class="n">advisor</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">advisors</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">advisor</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">	    <span class="c1">// 引介，基本用不到，这里代码省略...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">return</span> <span class="n">advisors</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>注意：这个方法是在一个循环中调入进来的，aspectInstanceFactory 中封装了当前遍历到的 beanName 以及 beanFactory 的信息。</p>
<ol>
<li>getAdvisorMethods()，获取到所有非切点表达式的方法，也就是不带 @PointCut 注解的方法。</li>
<li>遍历这些方法，getAdvisor() 将方法封装成 Advisor。</li>
</ol>
<p>getAdvisorMethods() 源码如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ReflectiveAspectJAdvisorFactory</span> <span class="kd">extends</span> <span class="n">AbstractAspectJAdvisorFactory</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">private</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Method</span><span class="o">&gt;</span> <span class="nf">getAdvisorMethods</span><span class="o">(</span><span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">aspectClass</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="kd">final</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Method</span><span class="o">&gt;</span> <span class="n">methods</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">		<span class="n">ReflectionUtils</span><span class="o">.</span><span class="na">doWithMethods</span><span class="o">(</span><span class="n">aspectClass</span><span class="o">,</span> <span class="n">method</span> <span class="o">-&gt;</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// Exclude pointcuts
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="k">if</span> <span class="o">(</span><span class="n">AnnotationUtils</span><span class="o">.</span><span class="na">getAnnotation</span><span class="o">(</span><span class="n">method</span><span class="o">,</span> <span class="n">Pointcut</span><span class="o">.</span><span class="na">class</span><span class="o">)</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">methods</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">method</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">});</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 先按照注解排序、如果是相同的注解，就按照方法名称排序。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">methods</span><span class="o">.</span><span class="na">sort</span><span class="o">(</span><span class="n">METHOD_COMPARATOR</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">methods</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>排序的代码有一点绕，嵌套的 lambda 表达式，是这么定义的（可以不看，实际业务中一般一个切面类只定义一个通知方法）：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ReflectiveAspectJAdvisorFactory</span> <span class="kd">extends</span> <span class="n">AbstractAspectJAdvisorFactory</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">Comparator</span><span class="o">&lt;</span><span class="n">Method</span><span class="o">&gt;</span> <span class="n">METHOD_COMPARATOR</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="kd">static</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="n">Comparator</span><span class="o">&lt;</span><span class="n">Method</span><span class="o">&gt;</span> <span class="n">adviceKindComparator</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ConvertingComparator</span><span class="o">&lt;&gt;(</span>
</span></span><span class="line"><span class="cl">				<span class="k">new</span> <span class="n">InstanceComparator</span><span class="o">&lt;&gt;(</span>
</span></span><span class="line"><span class="cl">						<span class="n">Around</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">Before</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">After</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">AfterReturning</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">AfterThrowing</span><span class="o">.</span><span class="na">class</span><span class="o">),</span>
</span></span><span class="line"><span class="cl">				<span class="o">(</span><span class="n">Converter</span><span class="o">&lt;</span><span class="n">Method</span><span class="o">,</span> <span class="n">Annotation</span><span class="o">&gt;)</span> <span class="n">method</span> <span class="o">-&gt;</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="n">AspectJAnnotation</span><span class="o">&lt;?&gt;</span> <span class="n">annotation</span> <span class="o">=</span>
</span></span><span class="line"><span class="cl">						<span class="n">AbstractAspectJAdvisorFactory</span><span class="o">.</span><span class="na">findAspectJAnnotationOnMethod</span><span class="o">(</span><span class="n">method</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">					<span class="k">return</span> <span class="o">(</span><span class="n">annotation</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">?</span> <span class="n">annotation</span><span class="o">.</span><span class="na">getAnnotation</span><span class="o">()</span> <span class="o">:</span> <span class="kc">null</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="o">});</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 先按照注解排序、如果是相同的注解，就按照方法名称排序。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">Comparator</span><span class="o">&lt;</span><span class="n">Method</span><span class="o">&gt;</span> <span class="n">methodNameComparator</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ConvertingComparator</span><span class="o">&lt;&gt;(</span><span class="n">Method</span><span class="o">::</span><span class="n">getName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="n">METHOD_COMPARATOR</span> <span class="o">=</span> <span class="n">adviceKindComparator</span><span class="o">.</span><span class="na">thenComparing</span><span class="o">(</span><span class="n">methodNameComparator</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>先使用 methodNameComparator 进行排序，如果排序结果相等，再使用 methodNameComparator 进行排序。每个比较器排序前都会使用 <code>Converter&lt;S, T&gt;</code> 转换其类型。</p>
<p>调用 getAdvisor()：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ReflectiveAspectJAdvisorFactory</span> <span class="kd">extends</span> <span class="n">AbstractAspectJAdvisorFactory</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="n">Advisor</span> <span class="nf">getAdvisor</span><span class="o">(</span><span class="n">Method</span> <span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">MetadataAwareAspectInstanceFactory</span> <span class="n">aspectInstanceFactory</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">			<span class="kt">int</span> <span class="n">declarationOrderInAspect</span><span class="o">,</span> <span class="n">String</span> <span class="n">aspectName</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="n">validate</span><span class="o">(</span><span class="n">aspectInstanceFactory</span><span class="o">.</span><span class="na">getAspectMetadata</span><span class="o">().</span><span class="na">getAspectClass</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 将当前方法、类型作为参数。获取PointCut对象，最重要的是从注解中获取表达式
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">AspectJExpressionPointcut</span> <span class="n">expressionPointcut</span> <span class="o">=</span> <span class="n">getPointcut</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">				<span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">aspectInstanceFactory</span><span class="o">.</span><span class="na">getAspectMetadata</span><span class="o">().</span><span class="na">getAspectClass</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">expressionPointcut</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 这里封装了切点表达式、切面类型、增强方法等信息。它是Advisor的一个实现类，最终会加入到代理实例的执行链中。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">return</span> <span class="k">new</span> <span class="n">InstantiationModelAwarePointcutAdvisorImpl</span><span class="o">(</span><span class="n">expressionPointcut</span><span class="o">,</span> <span class="n">candidateAdviceMethod</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">				<span class="k">this</span><span class="o">,</span> <span class="n">aspectInstanceFactory</span><span class="o">,</span> <span class="n">declarationOrderInAspect</span><span class="o">,</span> <span class="n">aspectName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol>
<li>获取到通知方法的 PointCut() 对象，如果有方法没有切点表达式，直接返回null，这种情况会被忽略，不被加入到 Advisor 列表中。</li>
<li>创建 Advisor 对象，封装切点表达式、通知方法等必要信息。</li>
</ol>
<h3 id="22-pointcut的获取">2.2 PointCut的获取</h3>
<p>关于如何获取 PointCut 的，源码如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ReflectiveAspectJAdvisorFactory</span> <span class="kd">extends</span> <span class="n">AbstractAspectJAdvisorFactory</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="kd">private</span> <span class="n">AspectJExpressionPointcut</span> <span class="nf">getPointcut</span><span class="o">(</span><span class="n">Method</span> <span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">candidateAspectClass</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 找到切面相关注解，并把注解信息解析出来，包装成AspectJAnnotation
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">AspectJAnnotation</span><span class="o">&lt;?&gt;</span> <span class="n">aspectJAnnotation</span> <span class="o">=</span>
</span></span><span class="line"><span class="cl">				<span class="n">AbstractAspectJAdvisorFactory</span><span class="o">.</span><span class="na">findAspectJAnnotationOnMethod</span><span class="o">(</span><span class="n">candidateAdviceMethod</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">aspectJAnnotation</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 将切点表达式封装到AspectJExpressionPointcut中。这里封装了切点表达式、切面类型等信息。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">AspectJExpressionPointcut</span> <span class="n">ajexp</span> <span class="o">=</span>
</span></span><span class="line"><span class="cl">				<span class="k">new</span> <span class="n">AspectJExpressionPointcut</span><span class="o">(</span><span class="n">candidateAspectClass</span><span class="o">,</span> <span class="k">new</span> <span class="n">String</span><span class="o">[</span><span class="n">0</span><span class="o">],</span> <span class="k">new</span> <span class="n">Class</span><span class="o">&lt;?&gt;[</span><span class="n">0</span><span class="o">]);</span>
</span></span><span class="line"><span class="cl">		<span class="n">ajexp</span><span class="o">.</span><span class="na">setExpression</span><span class="o">(</span><span class="n">aspectJAnnotation</span><span class="o">.</span><span class="na">getPointcutExpression</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">beanFactory</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">ajexp</span><span class="o">.</span><span class="na">setBeanFactory</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">beanFactory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">ajexp</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>接着看 findAspectJAnnotationOnMethod()：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AbstractAspectJAdvisorFactory</span> <span class="kd">implements</span> <span class="n">AspectJAdvisorFactory</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="cm">/** 这里定义了切面中支持的注解，前置、后置、环绕等通知类型 */</span>
</span></span><span class="line"><span class="cl">	<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">Class</span><span class="o">&lt;?&gt;[]</span> <span class="n">ASPECTJ_ANNOTATION_CLASSES</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Class</span><span class="o">&lt;?&gt;[]</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">Pointcut</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">Around</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">Before</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">After</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">AfterReturning</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">AfterThrowing</span><span class="o">.</span><span class="na">class</span><span class="o">};</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 步骤一
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="kd">protected</span> <span class="kd">static</span> <span class="n">AspectJAnnotation</span><span class="o">&lt;?&gt;</span> <span class="n">findAspectJAnnotationOnMethod</span><span class="o">(</span><span class="n">Method</span> <span class="n">method</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">for</span> <span class="o">(</span><span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">clazz</span> <span class="o">:</span> <span class="n">ASPECTJ_ANNOTATION_CLASSES</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 循环去找注解，并把注解信息解析出来，包装成AspectJAnnotation
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">AspectJAnnotation</span><span class="o">&lt;?&gt;</span> <span class="n">foundAnnotation</span> <span class="o">=</span> <span class="n">findAnnotation</span><span class="o">(</span><span class="n">method</span><span class="o">,</span> <span class="o">(</span><span class="n">Class</span><span class="o">&lt;</span><span class="n">Annotation</span><span class="o">&gt;)</span> <span class="n">clazz</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(</span><span class="n">foundAnnotation</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="k">return</span> <span class="n">foundAnnotation</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 步骤二
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="kd">private</span> <span class="kd">static</span> <span class="o">&lt;</span><span class="n">A</span> <span class="kd">extends</span> <span class="n">Annotation</span><span class="o">&gt;</span> <span class="n">AspectJAnnotation</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span> <span class="nf">findAnnotation</span><span class="o">(</span><span class="n">Method</span> <span class="n">method</span><span class="o">,</span> <span class="n">Class</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span> <span class="n">toLookFor</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 找的规则就是找注解的父注解，递归的方式去找，直到找到目标注解为止
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">A</span> <span class="n">result</span> <span class="o">=</span> <span class="n">AnnotationUtils</span><span class="o">.</span><span class="na">findAnnotation</span><span class="o">(</span><span class="n">method</span><span class="o">,</span> <span class="n">toLookFor</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">result</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 把注解里面对的信息解析出来，然后包装成AspectJAnnotation对象
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="k">return</span> <span class="k">new</span> <span class="n">AspectJAnnotation</span><span class="o">&lt;&gt;(</span><span class="n">result</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>从上面代码可以看到，ASPECTJ_ANNOTATION_CLASSES数组常量中，预定义了支持切点表达式的注解。如果能在当前方法上找到任何一种，就将这个切点信息封装成 AspectJAnnotation。</p>
<h3 id="23-advisor的初始化">2.3 Advisor的初始化</h3>
<p>这里还有一个重点，new InstantiationModelAwarePointcutAdvisorImpl 中的初始化过程。</p>
<p>构造函数源码如下：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="hl"><span class="lnt">18
</span></span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">final</span> <span class="kd">class</span> <span class="nc">InstantiationModelAwarePointcutAdvisorImpl</span>
</span></span><span class="line"><span class="cl">		<span class="kd">implements</span> <span class="n">InstantiationModelAwarePointcutAdvisor</span><span class="o">,</span> <span class="n">AspectJPrecedenceInformation</span><span class="o">,</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="nf">InstantiationModelAwarePointcutAdvisorImpl</span><span class="o">(</span><span class="n">AspectJExpressionPointcut</span> <span class="n">declaredPointcut</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">			<span class="n">Method</span> <span class="n">aspectJAdviceMethod</span><span class="o">,</span> <span class="n">AspectJAdvisorFactory</span> <span class="n">aspectJAdvisorFactory</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">			<span class="n">MetadataAwareAspectInstanceFactory</span> <span class="n">aspectInstanceFactory</span><span class="o">,</span> <span class="kt">int</span> <span class="n">declarationOrder</span><span class="o">,</span> <span class="n">String</span> <span class="n">aspectName</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 切点、切面名称、通知方法名称、等等一系列信息的封装
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">this</span><span class="o">.</span><span class="na">declaredPointcut</span> <span class="o">=</span> <span class="n">declaredPointcut</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 省略...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">aspectInstanceFactory</span><span class="o">.</span><span class="na">getAspectMetadata</span><span class="o">().</span><span class="na">isLazilyInstantiated</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 省略...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="o">}</span><span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// A singleton aspect.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="k">this</span><span class="o">.</span><span class="na">pointcut</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">declaredPointcut</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="k">this</span><span class="o">.</span><span class="na">lazy</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// TODO 重点，实例化的过程，这里封装了具体的Advice
</span></span></span><span class="line hl"><span class="cl"><span class="c1"></span>			<span class="k">this</span><span class="o">.</span><span class="na">instantiatedAdvice</span> <span class="o">=</span> <span class="n">instantiateAdvice</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">declaredPointcut</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span></span></span></code></pre></td></tr></table>
</div>
</div>
重点方法是 instantiateAdvice()，这里完成了 Advice 的创建。</p>
<p>继续向下跟踪代码：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">final</span> <span class="kd">class</span> <span class="nc">InstantiationModelAwarePointcutAdvisorImpl</span>
</span></span><span class="line"><span class="cl">		<span class="kd">implements</span> <span class="n">InstantiationModelAwarePointcutAdvisor</span><span class="o">,</span> <span class="n">AspectJPrecedenceInformation</span><span class="o">,</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">private</span> <span class="n">Advice</span> <span class="nf">instantiateAdvice</span><span class="o">(</span><span class="n">AspectJExpressionPointcut</span> <span class="n">pointcut</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// TODO 重点getAdvice
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">Advice</span> <span class="n">advice</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">aspectJAdvisorFactory</span><span class="o">.</span><span class="na">getAdvice</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">aspectJAdviceMethod</span><span class="o">,</span> <span class="n">pointcut</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">				<span class="k">this</span><span class="o">.</span><span class="na">aspectInstanceFactory</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="na">declarationOrder</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="na">aspectName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="o">(</span><span class="n">advice</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">?</span> <span class="n">advice</span> <span class="o">:</span> <span class="n">EMPTY_ADVICE</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>来到 getAdvice()：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ReflectiveAspectJAdvisorFactory</span> <span class="kd">extends</span> <span class="n">AbstractAspectJAdvisorFactory</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="n">Advice</span> <span class="nf">getAdvice</span><span class="o">(</span><span class="n">Method</span> <span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">AspectJExpressionPointcut</span> <span class="n">expressionPointcut</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">			<span class="n">MetadataAwareAspectInstanceFactory</span> <span class="n">aspectInstanceFactory</span><span class="o">,</span> <span class="kt">int</span> <span class="n">declarationOrder</span><span class="o">,</span> <span class="n">String</span> <span class="n">aspectName</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 获取切面类
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">candidateAspectClass</span> <span class="o">=</span> <span class="n">aspectInstanceFactory</span><span class="o">.</span><span class="na">getAspectMetadata</span><span class="o">().</span><span class="na">getAspectClass</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="n">validate</span><span class="o">(</span><span class="n">candidateAspectClass</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 查找切点表达式、五种通知增强注解。@Before等五种。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">AspectJAnnotation</span><span class="o">&lt;?&gt;</span> <span class="n">aspectJAnnotation</span> <span class="o">=</span>
</span></span><span class="line"><span class="cl">				<span class="n">AbstractAspectJAdvisorFactory</span><span class="o">.</span><span class="na">findAspectJAnnotationOnMethod</span><span class="o">(</span><span class="n">candidateAdviceMethod</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">aspectJAnnotation</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 省略部分代码...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">AbstractAspectJAdvice</span> <span class="n">springAdvice</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// TODO 执行判断，针对不同类型的通知，封装不同类型的Advice
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="c1">//  这几个Advice的实现类要记一下。主要以是否实现MethodInterceptor来区分，有的实现了，有的没有。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">switch</span> <span class="o">(</span><span class="n">aspectJAnnotation</span><span class="o">.</span><span class="na">getAnnotationType</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">case</span> <span class="n">AtPointcut</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">				<span class="k">if</span> <span class="o">(</span><span class="n">logger</span><span class="o">.</span><span class="na">isDebugEnabled</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="n">logger</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">&#34;Processing pointcut &#39;&#34;</span> <span class="o">+</span> <span class="n">candidateAdviceMethod</span><span class="o">.</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&#34;&#39;&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="o">}</span>
</span></span><span class="line"><span class="cl">				<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="k">case</span> <span class="n">AtAround</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">				<span class="n">springAdvice</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AspectJAroundAdvice</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">						<span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">expressionPointcut</span><span class="o">,</span> <span class="n">aspectInstanceFactory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="k">break</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="k">case</span> <span class="n">AtBefore</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">				<span class="n">springAdvice</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AspectJMethodBeforeAdvice</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">						<span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">expressionPointcut</span><span class="o">,</span> <span class="n">aspectInstanceFactory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="k">break</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="k">case</span> <span class="n">AtAfter</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">				<span class="n">springAdvice</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AspectJAfterAdvice</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">						<span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">expressionPointcut</span><span class="o">,</span> <span class="n">aspectInstanceFactory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="k">break</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="k">case</span> <span class="n">AtAfterReturning</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">				<span class="n">springAdvice</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AspectJAfterReturningAdvice</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">						<span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">expressionPointcut</span><span class="o">,</span> <span class="n">aspectInstanceFactory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="n">AfterReturning</span> <span class="n">afterReturningAnnotation</span> <span class="o">=</span> <span class="o">(</span><span class="n">AfterReturning</span><span class="o">)</span> <span class="n">aspectJAnnotation</span><span class="o">.</span><span class="na">getAnnotation</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">				<span class="k">if</span> <span class="o">(</span><span class="n">StringUtils</span><span class="o">.</span><span class="na">hasText</span><span class="o">(</span><span class="n">afterReturningAnnotation</span><span class="o">.</span><span class="na">returning</span><span class="o">()))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="n">springAdvice</span><span class="o">.</span><span class="na">setReturningName</span><span class="o">(</span><span class="n">afterReturningAnnotation</span><span class="o">.</span><span class="na">returning</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">				<span class="o">}</span>
</span></span><span class="line"><span class="cl">				<span class="k">break</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="k">case</span> <span class="n">AtAfterThrowing</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">				<span class="n">springAdvice</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AspectJAfterThrowingAdvice</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">						<span class="n">candidateAdviceMethod</span><span class="o">,</span> <span class="n">expressionPointcut</span><span class="o">,</span> <span class="n">aspectInstanceFactory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="n">AfterThrowing</span> <span class="n">afterThrowingAnnotation</span> <span class="o">=</span> <span class="o">(</span><span class="n">AfterThrowing</span><span class="o">)</span> <span class="n">aspectJAnnotation</span><span class="o">.</span><span class="na">getAnnotation</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">				<span class="k">if</span> <span class="o">(</span><span class="n">StringUtils</span><span class="o">.</span><span class="na">hasText</span><span class="o">(</span><span class="n">afterThrowingAnnotation</span><span class="o">.</span><span class="na">throwing</span><span class="o">()))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="n">springAdvice</span><span class="o">.</span><span class="na">setThrowingName</span><span class="o">(</span><span class="n">afterThrowingAnnotation</span><span class="o">.</span><span class="na">throwing</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">				<span class="o">}</span>
</span></span><span class="line"><span class="cl">				<span class="k">break</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="k">default</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">				<span class="k">throw</span> <span class="k">new</span> <span class="n">UnsupportedOperationException</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">						<span class="s">&#34;Unsupported advice type on method: &#34;</span> <span class="o">+</span> <span class="n">candidateAdviceMethod</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 省略...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">return</span> <span class="n">springAdvice</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><div class="details admonition note open">
        <div class="details-summary admonition-title">
            <i class="icon fas fa-pencil-alt fa-fw"></i>注意<i class="details-icon fas fa-angle-right fa-fw"></i>
        </div>
        <div class="details-content">
            <div class="admonition-content"><p>上面代码中，AspectJMethodBeforeAdvice 等几个Advice的实现要看一下，这里五个 Advice 实现，部分实现了MethodInterceptor。
实现了MethodInterceptor的：</p>
<ol>
<li>AspectJAroundAdvice</li>
<li>AspectJAfterAdvice</li>
<li>AspectJAfterThrowingAdvice</li>
</ol>
<p>未实现MethodInterceptor的 &ndash;&gt; 生成执行链时依赖的适配器：</p>
<ol>
<li>AspectJMethodBeforeAdvice &ndash;&gt; MethodBeforeAdviceAdapter</li>
<li>AspectJAfterReturningAdvice &ndash;&gt; AfterReturningAdviceAdapter</li>
</ol>
<p>另外还有一个未用到的Advice，也没有实现MethodInterceptor<br>
ThrowsAdvice &ndash;&gt; ThrowsAdviceAdapter</p>
<p>在生成搭理对象后，真正执行代理方法生成执行链的时候，实现了MethodInterceptor和未实现前者的，调用逻辑上有些差别。</p>
<p>实现了MethodInterceptor的，会直接加入到执行链，未实现的，会通过对应的适配器，调用 getInterceptor() 来获取 MethodInterceptor，然后将 MethodInterceptor 加入执行链。<del>（这些是后话）</del></p>
</div>
        </div>
    </div>
<h2 id="3-筛选出匹配的advisor">3 筛选出匹配的Advisor</h2>
<p>在获取到容器中所有的通知方法封装成的 Advisor 后，会调用 findAdvisorsThatCanApply()，将 beanName 放入到一个 ThreadLocal 类型的变量中，然后调用 findAdvisorsThatCanApply() 的重载方法。调用完毕之后，会将 ThreadLocal 变量中的 beanName置空。</p>
<p>findAdvisorsThatCanApply() 重载方法源码如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AopUtils</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="nf">findAdvisorsThatCanApply</span><span class="o">(</span><span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">candidateAdvisors</span><span class="o">,</span> <span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">clazz</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">candidateAdvisors</span><span class="o">.</span><span class="na">isEmpty</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="n">candidateAdvisors</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="n">List</span><span class="o">&lt;</span><span class="n">Advisor</span><span class="o">&gt;</span> <span class="n">eligibleAdvisors</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 先处理IntroductionAdvisor类型的增强
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">for</span> <span class="o">(</span><span class="n">Advisor</span> <span class="n">candidate</span> <span class="o">:</span> <span class="n">candidateAdvisors</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(</span><span class="n">candidate</span> <span class="k">instanceof</span> <span class="n">IntroductionAdvisor</span> <span class="o">&amp;&amp;</span> <span class="n">canApply</span><span class="o">(</span><span class="n">candidate</span><span class="o">,</span> <span class="n">clazz</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">eligibleAdvisors</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">candidate</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="kt">boolean</span> <span class="n">hasIntroductions</span> <span class="o">=</span> <span class="o">!</span><span class="n">eligibleAdvisors</span><span class="o">.</span><span class="na">isEmpty</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 再处理其他类型的增强
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">for</span> <span class="o">(</span><span class="n">Advisor</span> <span class="n">candidate</span> <span class="o">:</span> <span class="n">candidateAdvisors</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(</span><span class="n">candidate</span> <span class="k">instanceof</span> <span class="n">IntroductionAdvisor</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="c1">// already processed
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>				<span class="k">continue</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 我们自定义的切面，就会走到这里，使用已经封装的PointcutAdvisor，根据切点表达式进行匹配，具体的匹配过程，不用看。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="k">if</span> <span class="o">(</span><span class="n">canApply</span><span class="o">(</span><span class="n">candidate</span><span class="o">,</span> <span class="n">clazz</span><span class="o">,</span> <span class="n">hasIntroductions</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">eligibleAdvisors</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">candidate</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">eligibleAdvisors</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>调用 canApply()，这里进行了切点表达式与方法的匹配，如果匹配成功，将 Advisor 放入到集合中，否则就跳过。</p>
<p>当将所有的 Advisor 遍历完毕，最终匹配成功的 Advisor 会被筛选出来，放入到一个新的集合中，最终生成的执行链中的通知方法，就来自筛选后的 Advisor 集合（这个集合在创建代理前还有一次新增操作）。</p>
<p>看 canApply()，这里进行了匹配：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AopUtils</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 步骤一
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">boolean</span> <span class="nf">canApply</span><span class="o">(</span><span class="n">Advisor</span> <span class="n">advisor</span><span class="o">,</span> <span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">targetClass</span><span class="o">,</span> <span class="kt">boolean</span> <span class="n">hasIntroductions</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">advisor</span> <span class="k">instanceof</span> <span class="n">IntroductionAdvisor</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="o">((</span><span class="n">IntroductionAdvisor</span><span class="o">)</span> <span class="n">advisor</span><span class="o">).</span><span class="na">getClassFilter</span><span class="o">().</span><span class="na">matches</span><span class="o">(</span><span class="n">targetClass</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">advisor</span> <span class="k">instanceof</span> <span class="n">PointcutAdvisor</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">PointcutAdvisor</span> <span class="n">pca</span> <span class="o">=</span> <span class="o">(</span><span class="n">PointcutAdvisor</span><span class="o">)</span> <span class="n">advisor</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="c1">//切点表达式匹配。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="k">return</span> <span class="n">canApply</span><span class="o">(</span><span class="n">pca</span><span class="o">.</span><span class="na">getPointcut</span><span class="o">(),</span> <span class="n">targetClass</span><span class="o">,</span> <span class="n">hasIntroductions</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// It doesn&#39;t have a pointcut so we assume it applies.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="kd">static</span> <span class="kt">boolean</span> <span class="nf">canApply</span><span class="o">(</span><span class="n">Pointcut</span> <span class="n">pc</span><span class="o">,</span> <span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">targetClass</span><span class="o">,</span> <span class="kt">boolean</span> <span class="n">hasIntroductions</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 省略...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// 这里做了切入点表达式的匹配，匹配通过的返回true，具体的匹配过程不用看，不是重点。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">for</span> <span class="o">(</span><span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">clazz</span> <span class="o">:</span> <span class="n">classes</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">Method</span><span class="o">[]</span> <span class="n">methods</span> <span class="o">=</span> <span class="n">ReflectionUtils</span><span class="o">.</span><span class="na">getAllDeclaredMethods</span><span class="o">(</span><span class="n">clazz</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">for</span> <span class="o">(</span><span class="n">Method</span> <span class="n">method</span> <span class="o">:</span> <span class="n">methods</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="k">if</span> <span class="o">(</span><span class="n">introductionAwareMethodMatcher</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">?</span>
</span></span><span class="line"><span class="cl">						<span class="n">introductionAwareMethodMatcher</span><span class="o">.</span><span class="na">matches</span><span class="o">(</span><span class="n">method</span><span class="o">,</span> <span class="n">targetClass</span><span class="o">,</span> <span class="n">hasIntroductions</span><span class="o">)</span> <span class="o">:</span>
</span></span><span class="line"><span class="cl">						<span class="n">methodMatcher</span><span class="o">.</span><span class="na">matches</span><span class="o">(</span><span class="n">method</span><span class="o">,</span> <span class="n">targetClass</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">				<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>从上面代码可以看到，PointcutAdvisor 类型的，最终会遍历类中所有的方法，根据切点表达式，进行一一匹配，如果匹配成功，返回true，否则返回false。</p>
<h2 id="4-搜集容器中所有的advisor的流程">4 搜集容器中所有的Advisor的流程</h2>
<ol>
<li>父类调用流程：
<div class="mermaid" id="id-1"></div></li>
<li>子类调用流程：
<div class="mermaid" id="id-2"></div></li>
</ol></div>

        <div id="donateDiv"><a id="donate">打赏</a></div>
<div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2020-11-30</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/spring%E6%BA%90%E7%A0%81/">spring源码</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/posts/spring/09-aop-support-register/" class="prev" rel="prev" title="Spring源码 - AOP AbstractAutoProxyCreator的注册"><i class="fas fa-angle-left fa-fw"></i>Spring源码 - AOP AbstractAutoProxyCreator的注册</a>
            <a href="/posts/spring/11-aop-proxy-create-processor/" class="next" rel="next" title="Spring源码 - AOP 代理实例的创建">Spring源码 - AOP 代理实例的创建<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
<div id="comments"><div id="valine" class="comment"></div><noscript>
                Please enable JavaScript to view the comments powered by <a href="https://valine.js.org/">Valine</a>.
            </noscript></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.102.3">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2020 - 2024</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="https://wlizhi.cc" target="_blank">Wlizhi</a></span><span class="icp-splitter">&nbsp;|&nbsp;</span><br class="icp-br"/>
                    <span class="icp"><a href='https://beian.miit.gov.cn/' target='_blank'>豫ICP备17048163号<a/></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/valine/valine.min.css"><link rel="stylesheet" href="/lib/lightgallery/lightgallery.min.css"><link rel="stylesheet" href="/lib/mermaid/mermaid.min.css"><script type="text/javascript" src="/lib/valine/Valine.min.js"></script><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/autocomplete/autocomplete.min.js"></script><script type="text/javascript" src="/lib/algoliasearch/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/lightgallery/lightgallery.min.js"></script><script type="text/javascript" src="/lib/lightgallery/lg-thumbnail.min.js"></script><script type="text/javascript" src="/lib/lightgallery/lg-zoom.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/mermaid/mermaid.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":35},"comment":{"valine":{"appId":"4yIHB0Rs1r3ykDVxayI5nqj3-gzGzoHsz","appKey":"ALr4H1hlK94girLDEUCGmp3o","avatar":"mp","el":"#valine","emojiCDN":"https://cdn.jsdelivr.net/npm/emoji-datasource-google@5.0.1/img/google/64/","emojiMaps":{"100":"1f4af.png","alien":"1f47d.png","anger":"1f4a2.png","angry":"1f620.png","anguished":"1f627.png","astonished":"1f632.png","black_heart":"1f5a4.png","blue_heart":"1f499.png","blush":"1f60a.png","bomb":"1f4a3.png","boom":"1f4a5.png","broken_heart":"1f494.png","brown_heart":"1f90e.png","clown_face":"1f921.png","cold_face":"1f976.png","cold_sweat":"1f630.png","confounded":"1f616.png","confused":"1f615.png","cry":"1f622.png","crying_cat_face":"1f63f.png","cupid":"1f498.png","dash":"1f4a8.png","disappointed":"1f61e.png","disappointed_relieved":"1f625.png","dizzy":"1f4ab.png","dizzy_face":"1f635.png","drooling_face":"1f924.png","exploding_head":"1f92f.png","expressionless":"1f611.png","face_vomiting":"1f92e.png","face_with_cowboy_hat":"1f920.png","face_with_hand_over_mouth":"1f92d.png","face_with_head_bandage":"1f915.png","face_with_monocle":"1f9d0.png","face_with_raised_eyebrow":"1f928.png","face_with_rolling_eyes":"1f644.png","face_with_symbols_on_mouth":"1f92c.png","face_with_thermometer":"1f912.png","fearful":"1f628.png","flushed":"1f633.png","frowning":"1f626.png","ghost":"1f47b.png","gift_heart":"1f49d.png","green_heart":"1f49a.png","grimacing":"1f62c.png","grin":"1f601.png","grinning":"1f600.png","hankey":"1f4a9.png","hear_no_evil":"1f649.png","heart":"2764-fe0f.png","heart_decoration":"1f49f.png","heart_eyes":"1f60d.png","heart_eyes_cat":"1f63b.png","heartbeat":"1f493.png","heartpulse":"1f497.png","heavy_heart_exclamation_mark_ornament":"2763-fe0f.png","hole":"1f573-fe0f.png","hot_face":"1f975.png","hugging_face":"1f917.png","hushed":"1f62f.png","imp":"1f47f.png","innocent":"1f607.png","japanese_goblin":"1f47a.png","japanese_ogre":"1f479.png","joy":"1f602.png","joy_cat":"1f639.png","kiss":"1f48b.png","kissing":"1f617.png","kissing_cat":"1f63d.png","kissing_closed_eyes":"1f61a.png","kissing_heart":"1f618.png","kissing_smiling_eyes":"1f619.png","laughing":"1f606.png","left_speech_bubble":"1f5e8-fe0f.png","love_letter":"1f48c.png","lying_face":"1f925.png","mask":"1f637.png","money_mouth_face":"1f911.png","nauseated_face":"1f922.png","nerd_face":"1f913.png","neutral_face":"1f610.png","no_mouth":"1f636.png","open_mouth":"1f62e.png","orange_heart":"1f9e1.png","partying_face":"1f973.png","pensive":"1f614.png","persevere":"1f623.png","pleading_face":"1f97a.png","pouting_cat":"1f63e.png","purple_heart":"1f49c.png","rage":"1f621.png","relaxed":"263a-fe0f.png","relieved":"1f60c.png","revolving_hearts":"1f49e.png","right_anger_bubble":"1f5ef-fe0f.png","robot_face":"1f916.png","rolling_on_the_floor_laughing":"1f923.png","scream":"1f631.png","scream_cat":"1f640.png","see_no_evil":"1f648.png","shushing_face":"1f92b.png","skull":"1f480.png","skull_and_crossbones":"2620-fe0f.png","sleeping":"1f634.png","sleepy":"1f62a.png","slightly_frowning_face":"1f641.png","slightly_smiling_face":"1f642.png","smile":"1f604.png","smile_cat":"1f638.png","smiley":"1f603.png","smiley_cat":"1f63a.png","smiling_face_with_3_hearts":"1f970.png","smiling_imp":"1f608.png","smirk":"1f60f.png","smirk_cat":"1f63c.png","sneezing_face":"1f927.png","sob":"1f62d.png","space_invader":"1f47e.png","sparkling_heart":"1f496.png","speak_no_evil":"1f64a.png","speech_balloon":"1f4ac.png","star-struck":"1f929.png","stuck_out_tongue":"1f61b.png","stuck_out_tongue_closed_eyes":"1f61d.png","stuck_out_tongue_winking_eye":"1f61c.png","sunglasses":"1f60e.png","sweat":"1f613.png","sweat_drops":"1f4a6.png","sweat_smile":"1f605.png","thinking_face":"1f914.png","thought_balloon":"1f4ad.png","tired_face":"1f62b.png","triumph":"1f624.png","two_hearts":"1f495.png","unamused":"1f612.png","upside_down_face":"1f643.png","weary":"1f629.png","white_frowning_face":"2639-fe0f.png","white_heart":"1f90d.png","wink":"1f609.png","woozy_face":"1f974.png","worried":"1f61f.png","yawning_face":"1f971.png","yellow_heart":"1f49b.png","yum":"1f60b.png","zany_face":"1f92a.png","zipper_mouth_face":"1f910.png","zzz":"1f4a4.png"},"enableQQ":true,"highlight":true,"lang":"zh-cn","meta":["nick","mail","link"],"pageSize":5,"placeholder":"昵称栏输入QQ，会使用QQ昵称和头像、并自动补充QQ邮箱到邮箱栏。\r\n正确填写邮箱更容易及时得到回复。\r\n说点什么吧...","recordIP":true,"visitor":true}},"data":{"id-1":"graph LR;\n    B(findCandidateAdvisors) --\u003e|One| C(super.findCandidateAdvisors) \n    C --\u003e CA{findAdvisorBeans}\n    CA --\u003e|One| CAA(beanNamesForTypeIncludingAncestors)\n    CA --\u003e|Two| CAB(beanFactory.getBean)","id-2":"graph TB;\n    B(findCandidateAdvisors) --\u003e|Two| D(buildAspectJAdvisors)\n    D --\u003e|ForEach| E(beanNames)\n    E --\u003e F(isAspect) --\u003e|true| G{getAdvisors}\n    G --\u003e|One| GA(getAdvisorMethods)\n    GA --\u003e GAA1(doWithMethods) --\u003e GAA2(\"getAnnotation(method, Pointcut.class)\")\n    G --\u003e|Two| GB(getAdvisor)\n    GB --\u003e GBA(getPointcut)\n    GBA --\u003e GBAA(findAspectJAnnotationOnMethod) --\u003e|forEach| GBAA1(ASPECTJ_ANNOTATION_CLASSES)\n    GBAA1 --\u003e GBAA2(findAnnotation) --\u003e GBAA3(\"new AspectJAnnotation\u003c\u003e(result)\")"},"lightGallery":{"actualSize":false,"exThumbImage":"data-thumbnail","hideBarsDelay":2000,"selector":".lightgallery","speed":400,"thumbContHeight":80,"thumbWidth":80,"thumbnail":true},"search":{"algoliaAppID":"1U459KF21F","algoliaIndex":"LoveItSite","algoliaSearchKey":"0433ffe95d71201a4f7c3e04b0125ac5","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"}};</script><script type="text/javascript" src="/js/theme.min.js"></script><script type="text/javascript" src="/js/donate.js"></script></body>
</html>
